package com.audaque.springboot.foshanupload.web.webdemo.controller;

import com.audaque.springboot.foshanupload.web.webdemo.model.domain.*;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.io.xml.StaxDriver;
import com.thoughtworks.xstream.security.AnyTypePermission;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("xstream")
public class XStreamController {
    private static Student s = new Student();

    static {
        Employee employee = new Employee();
        employee.setId(1);
        employee.setName("1");
        OuterClass outerClass = new OuterClass();
        outerClass.setOuterVar(1);
        employee.setOuterClass(outerClass);
        s.setName("5wimming");
        s.setSex(2);
        Monitor monitor = null;
        try {
            monitor = new Monitor();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        monitor.setEmployee(employee);
        s.setMyObject(monitor);
        s.setMonitor(monitor);
        s.setEmployee(employee);

        Person person = new Person();
        person.setDescribe("1");
        person.setId("1");
        person.setName("1");
        person.setAge(1);
        person.setHeight(1);
        Country country = new Country();
        country.setId("1");
        country.setName("1");
        person.setPersonCountry(country);


        Person person2 = new Person();
        person2.setDescribe("2");
        person2.setId("2");
        person2.setName("2");
        person2.setAge(2);
        person2.setHeight(2);
        Country country2 = new Country();
        country2.setId("2");
        country2.setName("2");
        person2.setPersonCountry(country2);

        List<Person> personList = new ArrayList<>();
        personList.add(person);
        personList.add(person2);

        s.setPersonList(personList);


    }

    @ResponseBody
    @RequestMapping("serialize")
    public String serialize() throws IOException {
        XStream xstream = new XStream();
        // 序列化对象
        Person person = new Person();
        person.setDescribe("1");
        person.setId("1");
        person.setName("1");
        person.setAge(1);
        person.setHeight(1);
        Country country = new Country();
        country.setId("1");
        country.setName("1");
        person.setPersonCountry(country);
        String xmlOutput = xstream.toXML(person);
        return xmlOutput;

    }


    @ResponseBody
    @RequestMapping("parse")
    public Object parse() throws IOException {
        XStream xstream = new XStream();
        // 反序列化对象
        Object obj = xstream.fromXML(serialize());
        return obj;
    }


    @ResponseBody
    @RequestMapping("serialize1")
    public String serialize1() throws IOException {
        XStream xstream = new XStream();
        // 序列化对象
        String xmlOutput = xstream.toXML(s);
        return xmlOutput;

    }


    @ResponseBody
    @RequestMapping("parse1")
    public Object parse1() throws IOException {
        XStream xstream = new XStream();
        //尽量限制所需的最低权限 这条语句解决该问题:com.thoughtworks.xstream.security.ForbiddenClassException
        xstream.addPermission(AnyTypePermission.ANY);

        // 反序列化对象
        Object obj = xstream.fromXML(serialize1());
        return obj;
    }


    @ResponseBody
    @RequestMapping("serialize2")
    public void serialize2() throws IOException {
        ClassPathResource classPathResource = new ClassPathResource("xml/serialize2.xml");
        File file = new File(classPathResource.getURI());
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        XStream xstream = new XStream();
        // 序列化对象
        xstream.toXML(s, fileOutputStream);


    }


    @ResponseBody
    @RequestMapping("parse2")
    public Object parse2() throws IOException {
        XStream xstream = new XStream();
        //		尽量限制所需的最低权限 这条语句解决该问题:com.thoughtworks.xstream.security.ForbiddenClassException
        xstream.addPermission(AnyTypePermission.ANY);

        // 反序列化对象
        Object obj = xstream.fromXML("<com.audaque.springboot.foshanupload.web.webdemo.model.domain.Student>\n" +
                "  <myObject class=\"com.audaque.springboot.foshanupload.web.webdemo.model.domain.Monitor\">\n" +
                "    <hacker>test</hacker>\n" +
                "    <employee>\n" +
                "      <id>1</id>\n" +
                "      <name>1</name>\n" +
                "      <outerClass>\n" +
                "        <outerVar>1</outerVar>\n" +
                "      </outerClass>\n" +
                "    </employee>\n" +
                "  </myObject>\n" +
                "  <monitor reference=\"../myObject\"/>\n" +
                "  <employee reference=\"../myObject/employee\"/>\n" +
                "  <personList>\n" +
                "    <com.audaque.springboot.foshanupload.web.webdemo.model.domain.Person>\n" +
                "      <describe>1</describe>\n" +
                "      <id>1</id>\n" +
                "      <name>1</name>\n" +
                "      <age>1</age>\n" +
                "      <height>1.0</height>\n" +
                "      <personCountry>\n" +
                "        <id>1</id>\n" +
                "        <name>1</name>\n" +
                "      </personCountry>\n" +
                "      <salary>0</salary>\n" +
                "    </com.audaque.springboot.foshanupload.web.webdemo.model.domain.Person>\n" +
                "    <com.audaque.springboot.foshanupload.web.webdemo.model.domain.Person>\n" +
                "      <describe>2</describe>\n" +
                "      <id>2</id>\n" +
                "      <name>2</name>\n" +
                "      <age>2</age>\n" +
                "      <height>2.0</height>\n" +
                "      <personCountry>\n" +
                "        <id>2</id>\n" +
                "        <name>2</name>\n" +
                "      </personCountry>\n" +
                "      <salary>0</salary>\n" +
                "    </com.audaque.springboot.foshanupload.web.webdemo.model.domain.Person>\n" +
                "  </personList>\n" +
                "  <name>5wimming</name>\n" +
                "  <sex>2</sex>\n" +
                "</com.audaque.springboot.foshanupload.web.webdemo.model.domain.Student>");
        return obj;
    }


    @ResponseBody
    @RequestMapping("test1")
    public void test1() throws IOException {
        XStream xstream_1 = new XStream();//需要XPP3库
        //尽量限制所需的最低权限 这条语句解决该问题:com.thoughtworks.xstream.security.ForbiddenClassException
        xstream_1.addPermission(AnyTypePermission.ANY);
        XStream xstream_2 = new XStream(new DomDriver());//不需要XPP3库
        //尽量限制所需的最低权限 这条语句解决该问题:com.thoughtworks.xstream.security.ForbiddenClassException
        xstream_2.addPermission(AnyTypePermission.ANY);
        XStream xstream_3 = new XStream(new StaxDriver());//不需要XPP3库开始使用Java6
        //尽量限制所需的最低权限 这条语句解决该问题:com.thoughtworks.xstream.security.ForbiddenClassException
        xstream_3.addPermission(AnyTypePermission.ANY);
        //json
        XStream xstream_4 = new XStream(new JettisonMappedXmlDriver());//设置Json解析器
        xstream_4.setMode(XStream.NO_REFERENCES);//设置reference模型,不引用


        //XML序列化
        String xml_1 = xstream_1.toXML(s);
        String xml_2 = xstream_2.toXML(s);
        String xml_3 = xstream_3.toXML(s);
        String xml_4 = xstream_4.toXML(s);
        System.out.println(xml_1);
        System.out.println(xml_2);
        System.out.println(xml_3);
        System.out.println(xml_4);
        //XML反序列化
        Object bean1 =  xstream_1.fromXML(xml_1);
        System.out.println(bean1);
        Object bean2 =  xstream_2.fromXML(xml_2);
        System.out.println(bean2);
        Object bean3 =  xstream_3.fromXML(xml_3);
        System.out.println(bean3);
        Object bean4 =  xstream_4.fromXML(xml_4);
        System.out.println(bean4);
    }




}
